package com.neutech.maplestory.entity;

import com.neutech.maplestory.client.MapleStoryClient;
import com.neutech.maplestory.constant.Constant;
import com.neutech.maplestory.util.ImageUtil;

import java.awt.*;
import java.util.List;


public class Item extends AbstractMapleStoryObject{
    public Item(){}
    //道具类型
    public int type;
    public Image img ;
    public boolean jump;
    /**
     * 当前道具的库存
     */
    public  int qty = 1;
    public Item(MapleStoryClient msc ,int x, int y,int type){
        this.msc = msc;
        this.x = x;
        this.y = y;
        this.type = type;
        this.jump = true;
        this.live = true;
        switch (type){
            case 0:
                this.img = ImageUtil.getImage("HP_50");
                break;
            case 1:
                this.img = ImageUtil.getImage("HP_100");
                break;
            case 2:
                this.img = ImageUtil.getImage("HP_300");
                break;
            case 3:
                this.img = ImageUtil.getImage("MP_100");
                break;
            default:
                break;
        }
        this.width = 25;
        this.height = 25;

    }
    @Override
    public void draw(Graphics g) {
        if (!live){
            msc.items.remove(this);
        }
        g.drawImage(img,x,y,null);
        move();

    }

    @Override
    public void move() {
        if (jump) {
            jump(msc.grounds);
        } else {
            //自由落体
            jumpDown(msc.grounds);
        }
    }
    private double v0 = Constant.ITEM__INIT_JUMP_SPEED ;
    private double vt ;
    private double g = 9.8;
    private double delta_height;
    private double t = 0.7;
    /**
     * 跳跃的方法
     */
    public void jump(java.util.List<Ground> grounds) {
        if (!drop) {
            //竖直上抛
            vt = v0 - g * t;
            delta_height = v0 * t;
            v0 = vt;
            y -= delta_height;
            //什么时候往下跳
            if (vt <= 0) {
                vt = 0;
                drop = true;
            }
        } else {
            jumpDown(grounds);
        }

    }

    private boolean drop = true;

    public void jumpDown(List<Ground> grounds) {
        //掉的过程
        vt = v0 + g* t;
        delta_height = v0 * t;
        v0 = vt;
        y += delta_height;
        //什么时候停止自由落体
        for (int i = 0; i < grounds.size(); i++) {
            Ground ground = grounds.get(i);
            if (this.getRectangle().intersects(ground.getRectangle()) &&
                    this.y <= ground.y - this.height + 40) {
                //跳
                if (this.y >= ground.y - this.height) {
                    this.y = ground.y - this.height;
                    drop = false;
                    jump = false;
                    v0 = Constant.HERO_INIT_JUMP_SPEED;
                    vt = 0.0;
                    break;
                }
            } else {
                drop = true;
            }

        }
    }
}
